package controls {
	import UIFree.uif_component;
	
	import components.uif_tabItem;
	import components.uif_tabsBox;
	
	import flash.events.Event;
	import flash.geom.Rectangle;
	
	public class pjf_albums extends uif_tabsBox {
		public function pjf_albums(parent:uif_component, list: Array, a_doChangeSelectTab: Function,
								   ax:int=0, ay:int=0, awidth:int=0, 
								   aheight:int=0, atabwidth:int=0, styleClass:String='') {
			super(parent, ax, ay, awidth, aheight, atabwidth, styleClass);
			tabs.addEventListener(Event.CHANGE, a_doChangeSelectTab);
			assignList(list);
		}
		
		protected function maxWidthTab(list: Array): int {
			var itStyle: Object = app.styleByName(style['tabitem_class']); 
			return uif_component.maxWidth(list, itStyle.text_format.font, itStyle.text_format.size) + 
				(itStyle.padding?itStyle.padding:0) * 2 + 
				(itStyle.margin?itStyle.margin:0) * 2;
		}
		
		public function assignList(list: Array): void {
			var mwidth: int = maxWidthTab(list);
			var selectIndex: int = -1;
			tabs.assignList(list, mwidth + 20, function(_scroll: uif_component, inner: Rectangle, i: int): uif_tabItem {
				if (list[i].selected) selectIndex = i;
				return createItem(list, inner, i, style['tabitem_class'])
			});
			if (selectIndex > -1) tabs.selectIndex = selectIndex;
			else if (list.length > 0) tabs.selectIndex = 0;
		}
		
		protected function createItem(list: Array, inner: Rectangle, index: int, styleName: String): uif_tabItem {
			inner.inflate(inner.height / 3, 0);
			inner.x += inner.height / 3;
			if (list[index].hasOwnProperty('styleClass'))
				styleName = list[index].styleClass;
			var item: uif_tabItem = new uif_tabItem(tabs.scrollArea, inner, styleName);
			item.text = list[index].source;
			return item; 
		}
	}
}